package com.app.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.mapper.PlanBusinessMapper;
import com.app.model.BusinessModel;
import com.app.model.PlanBusinessModel;
import com.app.model.PlanFundsModel;
import com.app.model.StriveFundsModel;
import com.app.service.AttractInvestmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 招商引资
 */
@Controller
@RestController
@RequestMapping("/api/investment")
public class AttractInvestmentController {
    @Autowired
    private AttractInvestmentService attractInvestmentService;

    //查询全部
    @GetMapping("/all")
    public Result<Object> getAll( String status, int sort, String year) {
        PlanBusinessModel model = new PlanBusinessModel();
//        model.setPage((page - 1) * limit);
//        model.setLimit(limit);
        model.setStatusText(status);
        model.setSort(sort);
        model.setYear(year);
        return Result.success(attractInvestmentService.AttractList(model));
    }

    /*
        招商引资后台
     */
    //计划
    @GetMapping("/selectPlan")
    public Result<Object> selectPlan(int page, int limit, String year) {
        PlanBusinessModel model = new PlanBusinessModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setYear(year);
        return Result.success(attractInvestmentService.selectPlan(model));
    }
    @PostMapping("/savePlan")
    public Result<Object> savePlan(@RequestBody JSONObject json) {
        String year = json.get("year").toString();
        JSONArray jsonArray = JSONArray.parseArray(json.get("multiple").toString());
        PlanBusinessModel model = new PlanBusinessModel();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject multiple = jsonArray.getJSONObject(i);
            String taskPlan =  multiple.get("taskPlan").toString();
            Integer departmentId = (Integer) multiple.get("departmentId");
            model.setDepartmentId(departmentId);
            model.setTaskPlan(Double.valueOf(taskPlan));
            model.setYear(year);
            model.setCreateTime(LocalDateTime.now());
            model.setUpdateTime(LocalDateTime.now());
            attractInvestmentService.savePlan(model);
        }
        return Result.success("操作成功");
    }

    //完成情况
    // 查询列表
    @GetMapping("/completion")
    public Result<Object> selectCompletion(int page, int limit, String year) {
        BusinessModel model = new BusinessModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        if (year == null || year.equals("")) {
            return Result.success(attractInvestmentService.selectCompletion(model));
        }
        model.setYear(year);
        return Result.success(attractInvestmentService.selectCompletionByTime(model));
    }

    //添加
    @PostMapping("/add")
    public Result<Object> add(@RequestBody BusinessModel model) {
        model.setCreateTime(LocalDateTime.now());
        attractInvestmentService.add(model);
        return Result.success("添加成功");
    }


    @PostMapping("/update")
    public Result<Object> update(@RequestBody BusinessModel model) {
        model.setUpdateTime(LocalDateTime.now());
        attractInvestmentService.updateFunds(model);
        return Result.success("修改成功");
    }

    //删除
    @GetMapping("/delete")
    public Result<Object> delete(int id) {
        attractInvestmentService.delete(id);
        return Result.success("删除成功");
    }
}
